﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ITask
{
    /// <summary>
    /// 任务执行完毕时间处理的代理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void RunTaskCompletedEventHandler(Object sender, EventArgs e);

    /// <summary>
    /// 任务基类
    /// <remarks>没有添加日志支持</remarks>
    /// </summary>
    public abstract class TaskBase : MarshalByRefObject, ITask
    {
        #region Fields
        private string id;
        private TaskConfig taskConfig;
        private TaskStatus taskStatus;
        #endregion

        #region Properties
        /// <summary>
        /// 服务标签
        /// </summary>
        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        /// <summary>
        /// 服务配置
        /// </summary>
        public TaskConfig Config
        {
            get
            {
                return taskConfig;
            }
            set
            {
                taskConfig = value;
            }
        }

        /// <summary>
        /// 服务状态
        /// </summary>
        public TaskStatus Status
        {
            get { return taskStatus; }
        }

        /// <summary>
        /// 上次服务运行时间
        /// </summary>
        public DateTime LastRunTime
        {
            get
            {
                if (taskConfig != null)
                {
                    DateTime lastRunTime;
                    if (!DateTime.TryParse(taskConfig.Savings.LastRunTime, out lastRunTime))
                    {
                        lastRunTime = DateTime.MinValue;
                    }
                    return lastRunTime;
                }
                else
                {
                    return DateTime.MinValue;
                }
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// 构造式
        /// </summary>
        public TaskBase()
        {
            taskStatus = TaskStatus.Free;
        }
        #endregion

        #region Methods
        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="args">参数</param>
        public abstract void Run(object[] args);

        /// <summary>
        /// 异步执行
        /// </summary>
        /// <param name="args">参数</param>
        public abstract IAsyncResult RunAsync(object[] args);

        /// <summary>
        /// 结束
        /// </summary>
        public abstract void Stop();

        /// <summary>
        /// 是否可以结束
        /// </summary>
        /// <returns>取得是否可以结束</returns>
        public abstract bool CanStop();

        /// <summary>
        /// 清理
        /// </summary>
        public abstract void Dispose();
        #endregion

        #region Events
        /// <summary>
        /// 异步执行任务完毕所触发的事件
        /// </summary>
        public event RunTaskCompletedEventHandler RunTaskCompleted;
        #endregion
    }
}
